import { FormattedStringValueExtracter } from './FormattedStringValueExtracter';
import { environment } from '@env/environment';

export class SubdomainTenancyNameFinder {
  public readonly tenancyNamePlaceHolderInUrl = '{TENANCY_NAME}';
  getCurrentTenancyNameOrNull(): string {
    if (environment.CLIENT_URL.indexOf(this.tenancyNamePlaceHolderInUrl) < 0) {
      // Web site does not support subdomain tenant name
      return null;
    }

    const currentRootAddress = document.location.href;

    const formattedStringValueExtracter = new FormattedStringValueExtracter();
    const values: any[] = formattedStringValueExtracter.IsMatch(currentRootAddress, environment.CLIENT_URL);
    if (!values.length) {
      return null;
    }

    return values[0];
  }
}
